<template>
  <div class="admin-dashboard">
    <!-- 侧边导航栏 -->
    <div class="admin-sidebar">
      <div class="sidebar-header">
        <h2>🎛️ 管理后台</h2>
      </div>
      
      <div class="sidebar-menu">
        <div 
          class="menu-item"
          :class="{ active: activeMenu === 'statistics' }"
          @click="switchMenu('statistics')"
        >
          <BarChartOutlined />
          <span>销售统计</span>
        </div>
        
        <div 
          class="menu-item"
          :class="{ active: activeMenu === 'products' }"
          @click="switchMenu('products')"
        >
          <ShoppingOutlined />
          <span>商品管理</span>
        </div>
        
        <div 
          class="menu-item"
          :class="{ active: activeMenu === 'orders' }"
          @click="switchMenu('orders')"
        >
          <OrderedListOutlined />
          <span>订单管理</span>
        </div>
      </div>
      
      <div class="sidebar-footer">
        <a-button type="link" @click="$router.push('/')" class="back-btn">
          <ArrowLeftOutlined />
          返回前台
        </a-button>
      </div>
    </div>

    <!-- 主内容区域 -->
    <div class="admin-content">
      <div class="content-header">
        <h1>{{ getMenuTitle() }}</h1>
        <div class="header-actions">
          <a-button type="primary" @click="refreshData">
            <ReloadOutlined />
            刷新数据
          </a-button>
        </div>
      </div>

      <!-- 销售统计页面 -->
      <div v-if="activeMenu === 'statistics'" class="content-section">
        <div class="stats-cards">
          <div class="stat-card">
            <div class="stat-icon sales">
              <DollarOutlined />
            </div>
            <div class="stat-info">
              <h3>今日销售额</h3>
              <p class="stat-value">¥{{ todaySales.toFixed(2) }}</p>
              <span class="stat-change positive">+12.5%</span>
            </div>
          </div>
          
          <div class="stat-card">
            <div class="stat-icon orders">
              <ShoppingCartOutlined />
            </div>
            <div class="stat-info">
              <h3>今日订单数</h3>
              <p class="stat-value">{{ todayOrders }}</p>
              <span class="stat-change positive">+8.3%</span>
            </div>
          </div>
          
          <div class="stat-card">
            <div class="stat-icon users">
              <UserOutlined />
            </div>
            <div class="stat-info">
              <h3>新增用户</h3>
              <p class="stat-value">{{ newUsers }}</p>
              <span class="stat-change positive">+15.2%</span>
            </div>
          </div>
          
          <div class="stat-card">
            <div class="stat-icon products">
              <GiftOutlined />
            </div>
            <div class="stat-info">
              <h3>商品总数</h3>
              <p class="stat-value">{{ totalProducts }}</p>
              <span class="stat-change neutral">+0%</span>
            </div>
          </div>
        </div>
        
        <div class="charts-section">
          <div class="chart-card">
            <h3>销售趋势</h3>
            <div class="chart-container">
              <v-chart class="chart" :option="salesTrendOption" />
            </div>
          </div>
          
          <div class="chart-card">
            <h3>商品分类销售</h3>
            <div class="chart-container">
              <v-chart class="chart" :option="categorySalesOption" />
            </div>
          </div>
        </div>
      </div>

      <!-- 商品管理页面 -->
      <div v-if="activeMenu === 'products'" class="content-section">
        <div class="section-header">
          <div class="header-left">
            <h2>商品管理</h2>
            <span class="product-count">共 {{ products.length }} 件商品</span>
          </div>
          <a-button type="primary" @click="addProduct">
            <PlusOutlined />
            添加商品
          </a-button>
        </div>
        
        <div class="products-table">
          <a-table 
            :columns="productColumns" 
            :data-source="products"
            :pagination="{ pageSize: 10 }"
            row-key="id"
          >
            <template #bodyCell="{ column, record }">
              <template v-if="column.key === 'image'">
                <img :src="record.image" :alt="record.name" class="product-image" />
              </template>
              <template v-else-if="column.key === 'status'">
                <a-tag :color="record.status === 'active' ? 'green' : 'red'">
                  {{ record.status === 'active' ? '上架' : '下架' }}
                </a-tag>
              </template>
              <template v-else-if="column.key === 'actions'">
                <a-space>
                  <a-button type="link" size="small" @click="editProduct(record)">
                    <EditOutlined />
                    编辑
                  </a-button>
                  <a-button type="link" size="small" @click="toggleProductStatus(record)">
                    {{ record.status === 'active' ? '下架' : '上架' }}
                  </a-button>
                  <a-button type="link" size="small" danger @click="deleteProduct(record)">
                    <DeleteOutlined />
                    删除
                  </a-button>
                </a-space>
              </template>
            </template>
          </a-table>
        </div>
      </div>

      <!-- 订单管理页面 -->
      <div v-if="activeMenu === 'orders'" class="content-section">
        <div class="section-header">
          <div class="header-left">
            <h2>订单管理</h2>
            <span class="order-count">共 {{ orders.length }} 个订单</span>
          </div>
          <div class="header-actions">
            <a-select v-model:value="orderStatusFilter" style="width: 120px" placeholder="订单状态">
              <a-select-option value="">全部</a-select-option>
              <a-select-option value="pending">待付款</a-select-option>
              <a-select-option value="paid">已付款</a-select-option>
              <a-select-option value="shipped">已发货</a-select-option>
              <a-select-option value="completed">已完成</a-select-option>
            </a-select>
          </div>
        </div>
        
        <div class="orders-table">
          <a-table 
            :columns="orderColumns" 
            :data-source="filteredOrders"
            :pagination="{ pageSize: 10 }"
            row-key="id"
          >
            <template #bodyCell="{ column, record }">
              <template v-if="column.key === 'status'">
                <a-tag :color="getStatusColor(record.status)">
                  {{ getStatusText(record.status) }}
                </a-tag>
              </template>
              <template v-else-if="column.key === 'actions'">
                <a-space>
                  <a-button type="link" size="small" @click="viewOrder(record)">
                    <EyeOutlined />
                    查看
                  </a-button>
                  <a-button 
                    v-if="record.status === 'paid'" 
                    type="link" 
                    size="small" 
                    @click="shipOrder(record)"
                  >
                    <CarOutlined />
                    发货
                  </a-button>
                </a-space>
              </template>
            </template>
          </a-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { LineChart, PieChart } from 'echarts/charts'
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent
} from 'echarts/components'
import VChart from 'vue-echarts'
import { 
  BarChartOutlined, 
  ShoppingOutlined, 
  OrderedListOutlined,
  ArrowLeftOutlined,
  ReloadOutlined,
  DollarOutlined,
  ShoppingCartOutlined,
  UserOutlined,
  GiftOutlined,
  LineChartOutlined,
  PieChartOutlined,
  PlusOutlined,
  EditOutlined,
  DeleteOutlined,
  EyeOutlined,
  CarOutlined
} from '@ant-design/icons-vue'

// 注册 ECharts 组件
use([
  CanvasRenderer,
  LineChart,
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent
])

const activeMenu = ref('statistics')
const orderStatusFilter = ref('')

// 统计数据
const todaySales = ref(12580.50)
const todayOrders = ref(156)
const newUsers = ref(23)
const totalProducts = ref(89)

// 销售趋势图表配置
const salesTrendOption = ref({
  title: {
    text: '近7天销售趋势',
    left: 'center',
    textStyle: {
      fontSize: 14,
      fontWeight: 'normal',
      color: '#666'
    }
  },
  tooltip: {
    trigger: 'axis',
    formatter: '{b}<br/>销售额: ¥{c}'
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['1月9日', '1月10日', '1月11日', '1月12日', '1月13日', '1月14日', '1月15日'],
    axisLine: {
      lineStyle: {
        color: '#ddd'
      }
    },
    axisLabel: {
      color: '#666'
    }
  },
  yAxis: {
    type: 'value',
    axisLine: {
      lineStyle: {
        color: '#ddd'
      }
    },
    axisLabel: {
      color: '#666',
      formatter: '¥{value}'
    },
    splitLine: {
      lineStyle: {
        color: '#f0f0f0'
      }
    }
  },
  series: [
    {
      name: '销售额',
      type: 'line',
      smooth: true,
      data: [8200, 9320, 9010, 9340, 12900, 13300, 12580],
      itemStyle: {
        color: '#667eea'
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: 'rgba(102, 126, 234, 0.3)'
            },
            {
              offset: 1,
              color: 'rgba(102, 126, 234, 0.1)'
            }
          ]
        }
      },
      lineStyle: {
        width: 3
      },
      symbol: 'circle',
      symbolSize: 6
    }
  ]
})

// 商品分类销售饼图配置
const categorySalesOption = ref({
  title: {
    text: '商品分类销售占比',
    left: 'center',
    textStyle: {
      fontSize: 14,
      fontWeight: 'normal',
      color: '#666'
    }
  },
  tooltip: {
    trigger: 'item',
    formatter: '{a} <br/>{b}: {c} ({d}%)'
  },
  legend: {
    orient: 'vertical',
    left: 'left',
    top: 'middle',
    textStyle: {
      color: '#666'
    }
  },
  series: [
    {
      name: '销售占比',
      type: 'pie',
      radius: ['40%', '70%'],
      center: ['60%', '50%'],
      avoidLabelOverlap: false,
      itemStyle: {
        borderRadius: 10,
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        show: false,
        position: 'center'
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '18',
          fontWeight: 'bold',
          color: '#333'
        }
      },
      labelLine: {
        show: false
      },
      data: [
        {
          value: 1048,
          name: '护肤',
          itemStyle: {
            color: '#667eea'
          }
        },
        {
          value: 735,
          name: '彩妆',
          itemStyle: {
            color: '#f093fb'
          }
        },
        {
          value: 580,
          name: '香水',
          itemStyle: {
            color: '#4facfe'
          }
        },
        {
          value: 484,
          name: '工具',
          itemStyle: {
            color: '#43e97b'
          }
        },
        {
          value: 300,
          name: '洗护',
          itemStyle: {
            color: '#fa709a'
          }
        }
      ]
    }
  ]
})

// 商品数据
const products = ref([
  {
    id: 1,
    name: '兰蔻小黑瓶精华液',
    price: 899,
    stock: 50,
    sales: 1234,
    status: 'active',
    image: 'https://via.placeholder.com/60x60/ff7675/ffffff?text=兰蔻'
  },
  {
    id: 2,
    name: '雅诗兰黛小棕瓶',
    price: 699,
    stock: 30,
    sales: 2345,
    status: 'active',
    image: 'https://via.placeholder.com/60x60/74b9ff/ffffff?text=雅诗兰黛'
  },
  {
    id: 3,
    name: 'SK-II神仙水',
    price: 1299,
    stock: 20,
    sales: 890,
    status: 'inactive',
    image: 'https://via.placeholder.com/60x60/55a3ff/ffffff?text=SK-II'
  }
])

// 订单数据
const orders = ref([
  {
    id: 'ORD001',
    customer: '张三',
    amount: 899,
    status: 'paid',
    createTime: '2024-01-15 14:30:00',
    products: ['兰蔻小黑瓶精华液']
  },
  {
    id: 'ORD002',
    customer: '李四',
    amount: 1398,
    status: 'shipped',
    createTime: '2024-01-15 13:20:00',
    products: ['雅诗兰黛小棕瓶', 'SK-II神仙水']
  },
  {
    id: 'ORD003',
    customer: '王五',
    amount: 699,
    status: 'pending',
    createTime: '2024-01-15 12:15:00',
    products: ['雅诗兰黛小棕瓶']
  }
])

// 商品表格列配置
const productColumns = [
  {
    title: '商品图片',
    dataIndex: 'image',
    key: 'image',
    width: 80
  },
  {
    title: '商品名称',
    dataIndex: 'name',
    key: 'name'
  },
  {
    title: '价格',
    dataIndex: 'price',
    key: 'price',
    render: (price: number) => `¥${price}`
  },
  {
    title: '库存',
    dataIndex: 'stock',
    key: 'stock'
  },
  {
    title: '销量',
    dataIndex: 'sales',
    key: 'sales'
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status'
  },
  {
    title: '操作',
    key: 'actions',
    width: 200
  }
]

// 订单表格列配置
const orderColumns = [
  {
    title: '订单号',
    dataIndex: 'id',
    key: 'id'
  },
  {
    title: '客户',
    dataIndex: 'customer',
    key: 'customer'
  },
  {
    title: '金额',
    dataIndex: 'amount',
    key: 'amount',
    render: (amount: number) => `¥${amount}`
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status'
  },
  {
    title: '下单时间',
    dataIndex: 'createTime',
    key: 'createTime'
  },
  {
    title: '操作',
    key: 'actions',
    width: 150
  }
]

// 过滤后的订单
const filteredOrders = computed(() => {
  if (!orderStatusFilter.value) return orders.value
  return orders.value.filter(order => order.status === orderStatusFilter.value)
})

const switchMenu = (menu: string) => {
  activeMenu.value = menu
}

const getMenuTitle = () => {
  const titles = {
    statistics: '销售统计',
    products: '商品管理',
    orders: '订单管理'
  }
  return titles[activeMenu.value as keyof typeof titles] || ''
}

const refreshData = () => {
  // 模拟刷新数据
  console.log('刷新数据')
}

const addProduct = () => {
  console.log('添加商品')
}

const editProduct = (product: any) => {
  console.log('编辑商品:', product)
}

const toggleProductStatus = (product: any) => {
  product.status = product.status === 'active' ? 'inactive' : 'active'
}

const deleteProduct = (product: any) => {
  console.log('删除商品:', product)
}

const viewOrder = (order: any) => {
  console.log('查看订单:', order)
}

const shipOrder = (order: any) => {
  order.status = 'shipped'
  console.log('发货订单:', order)
}

const getStatusColor = (status: string) => {
  const colors = {
    pending: 'orange',
    paid: 'blue',
    shipped: 'purple',
    completed: 'green'
  }
  return colors[status as keyof typeof colors] || 'default'
}

const getStatusText = (status: string) => {
  const texts = {
    pending: '待付款',
    paid: '已付款',
    shipped: '已发货',
    completed: '已完成'
  }
  return texts[status as keyof typeof texts] || status
}
</script>

<style scoped>
.admin-dashboard {
  display: flex;
  height: 100vh;
  background: #f5f5f5;
}

.admin-sidebar {
  width: 240px;
  background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.sidebar-header {
  padding: 24px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: white;
}

.sidebar-menu {
  flex: 1;
  padding: 20px 0;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin: 4px 12px;
  border-radius: 8px;
  color: rgba(255, 255, 255, 0.8);
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  transform: translateX(4px);
}

.menu-item.active {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.menu-item .anticon {
  font-size: 18px;
  width: 20px;
  text-align: center;
}

.menu-item span {
  font-size: 14px;
  font-weight: 500;
}

.sidebar-footer {
  padding: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.back-btn {
  color: rgba(255, 255, 255, 0.8);
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.back-btn:hover {
  color: white;
}

.admin-content {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  background: #f5f5f5;
}

.content-header {
  background: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.content-header h1 {
  margin: 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.content-section {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 24px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.section-header h2 {
  margin: 0;
  color: #333;
  font-size: 20px;
  font-weight: 600;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.product-count,
.order-count {
  color: #666;
  font-size: 14px;
  background: #f5f5f5;
  padding: 4px 8px;
  border-radius: 4px;
}

/* 统计卡片样式 */
.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.stat-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border: 1px solid #f0f0f0;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
  font-size: 24px;
  color: white;
}

.stat-icon.sales {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.orders {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.users {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.products {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-info h3 {
  margin: 0 0 8px 0;
  color: #666;
  font-size: 14px;
  font-weight: 500;
}

.stat-value {
  font-size: 28px;
  font-weight: 700;
  color: #333;
  margin: 0 0 8px 0;
}

.stat-change {
  font-size: 12px;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 12px;
}

.stat-change.positive {
  color: #52c41a;
  background: rgba(82, 196, 26, 0.1);
}

.stat-change.negative {
  color: #f5222d;
  background: rgba(245, 34, 45, 0.1);
}

.stat-change.neutral {
  color: #909399;
  background: rgba(144, 147, 153, 0.1);
}

/* 图表区域样式 */
.charts-section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 24px;
}

.chart-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border: 1px solid #f0f0f0;
}

.chart-card h3 {
  margin: 0 0 20px 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
}

.chart-container {
  height: 300px;
}

.chart {
  height: 100%;
}

/* 表格样式 */
.products-table,
.orders-table {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.product-image {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid #f0f0f0;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .admin-sidebar {
    width: 200px;
  }
  
  .stats-cards {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .charts-section {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 992px) {
  .admin-dashboard {
    flex-direction: column;
    height: auto;
  }
  
  .admin-sidebar {
    width: 100%;
    height: auto;
  }
  
  .sidebar-menu {
    display: flex;
    padding: 12px 20px;
  }
  
  .menu-item {
    flex: 1;
    justify-content: center;
    margin: 0 4px;
    padding: 12px 16px;
  }
  
  .sidebar-footer {
    display: none;
  }
  
  .admin-content {
    padding: 16px;
  }
  
  .stats-cards {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .admin-content {
    padding: 12px;
  }
  
  .content-header {
    padding: 16px;
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .content-header h1 {
    font-size: 20px;
  }
  
  .content-section {
    padding: 16px;
  }
  
  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .stat-card {
    padding: 16px;
  }
  
  .stat-value {
    font-size: 24px;
  }
  
  .chart-card {
    padding: 16px;
  }
  
  .chart-container {
    height: 200px;
  }
}

@media (max-width: 480px) {
  .sidebar-menu {
    flex-direction: column;
    padding: 8px 12px;
  }
  
  .menu-item {
    margin: 2px 0;
    padding: 10px 12px;
  }
  
  .admin-content {
    padding: 8px;
  }
  
  .content-header {
    padding: 12px;
  }
  
  .content-section {
    padding: 12px;
  }
  
  .stat-card {
    padding: 12px;
  }
  
  .stat-icon {
    width: 50px;
    height: 50px;
    font-size: 20px;
  }
  
  .stat-value {
    font-size: 20px;
  }
}
</style> 